package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.invoke.MutableCallSite;
import java.util.UUID;
/**
 * 完成数据上传及下载功能
 */
@Controller
public class WordController {
    @GetMapping("/toupfile")
    public String setUpfile(){
        System.out.println("进入访问模板界面方法");
        String a="10";
        return "upfile";
    }
    @PostMapping ("/upfile")
    public String upfile(MultipartFile[] fileload, Model model){
        System.out.println("进入上传文件方法");
        model.addAttribute("uploadStatus","上传成功");
        System.out.println(fileload.length);
        for (MultipartFile file : fileload) {
           String fileName = file.getOriginalFilename();
           fileName= UUID.randomUUID()+"_"+fileName;
           String dirPath="D:/file/";
            File filePath=new File(dirPath);
            if(!filePath.exists()){
                filePath.mkdirs();
            }
            try {
                file.transferTo(new File(dirPath+fileName));
            } catch (IOException e) {
                System.out.println("将上传的文件拷贝到指定目录时出错，原因："+e.getMessage());
                model.addAttribute("uploadStatus","上传失败");
            }
        }
        return "upfile";
    }
}
